package com.jsb.laundry.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsb.laundry.common.exception.BusinessException;
import com.jsb.laundry.common.exception.ErrorCode;
import com.jsb.laundry.dao.JsbPostLikesMapper;
import com.jsb.laundry.model.entity.JsbPostLikes;
import com.jsb.laundry.model.entity.JsbPosts;
import com.jsb.laundry.service.JsbPostLikesService;
import com.jsb.laundry.service.JsbPostsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author ching
* @description 针对表【jsb_post_likes(帖子点赞关系表)】的数据库操作Service实现
* @createDate 2023-08-09 14:06:04
*/
@Service
public class JsbPostLikesServiceImpl extends ServiceImpl<JsbPostLikesMapper, JsbPostLikes>
    implements JsbPostLikesService {
    @Resource
    private JsbPostsService postService;

    /**
     * 点赞
     *
     * @param postId
     * @param userId
     * @return
     */
    @Override
    public int doPostLike(Integer postId, String userId) {
        // 判断实体是否存在，根据类别获取实体
        JsbPosts post = postService.getById(postId);
        if (post == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        // 是否已点赞
        // 每个用户串行点赞
        // 锁必须要包裹住事务方法
        JsbPostLikesService postLikeService = (JsbPostLikesService) AopContext.currentProxy();
        synchronized (userId.intern()) {
            return postLikeService.doPostLikeInner(userId, postId);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int doPostLikeInner(String userId, Integer postId) {
        JsbPostLikes postLike = new JsbPostLikes();
        postLike.setUserId(userId);
        postLike.setPostId(postId);
        QueryWrapper<JsbPostLikes> likeQueryWrapper = new QueryWrapper<>(postLike);
        JsbPostLikes oldPostThumb = this.getOne(likeQueryWrapper);
        boolean result;
        // 已点赞
        if (oldPostThumb != null) {
            result = this.remove(likeQueryWrapper);
            if (result) {
                // 点赞数 - 1
                result = postService.update()
                        .eq("id", postId)
                        .gt("likes", 0)
                        .setSql("likes = likes - 1")
                        .update();
                return result ? -1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        } else {
            // 未点赞
            result = this.save(postLike);
            if (result) {
                // 点赞数 + 1
                result = postService.update()
                        .eq("id", postId)
                        .setSql("likes = likes + 1")
                        .update();
                return result ? 1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
    }

    @Override
    public Page<JsbPosts> listLikePostByPage(IPage<JsbPosts> page, Wrapper<JsbPosts> queryWrapper, String favourUserId) {
        if (StringUtils.isAnyBlank(favourUserId)) {
            return new Page<>();
        }
        return baseMapper.listLikePostByPage(page, queryWrapper, favourUserId);
    }
}




